package com.dd.cloud.user.controller;


import com.dd.cloud.user.DTO.ca.ConcatSignAddReq;
import com.dd.cloud.user.DTO.ca.VisaWaitFileUploadReq;
import com.dd.cloud.user.service.ca.ICaDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 医生CA账号信息 前端控制器
 * </p>
 *
 * @author czg
 * @since 2024-01-22
 */
@RestController
@RequestMapping("/ca")
@Validated
public class CaController {

    @Autowired
    private ICaDoctorService caDoctorService;

    /**
     * 个人认证
     *
     */
    @PostMapping("/person/auth")
    public ResponseEntity<Void> personAuth() {
        caDoctorService.authCaDoctor();
        return ResponseEntity.ok().build();
    }

    /**
     * 重新发送验证码
     *
     */
    @PostMapping("/captcha/resend")
    public ResponseEntity<Void> captchaResend(@NotNull(message = "用户id不能为空") Integer id) {
        caDoctorService.captchaResend(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 效验验证码
     *
     */
    @PostMapping("/captcha/verify")
    public ResponseEntity<Void> captchaVerify(@NotNull(message = "用户id不能为空") Integer id,
                                              @NotBlank(message = "验证码不能为空") String captcha) {
        caDoctorService.captchaVerify(id, captcha);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加个人用户
     *
     */
    @PostMapping("/person/add")
    public ResponseEntity<Void> personAdd(@NotBlank(message = "用户账号不能为空") String account,
                                          @NotBlank(message = "用户密码不能为空") String signPwd) {
        caDoctorService.addPersonalUser(account, signPwd);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加个人用户印章
     *
     */
    @PostMapping("/seal/add")
    public ResponseEntity<Void> sealAdd(@NotBlank(message = "用户账号不能为空") String account) {
        caDoctorService.createSeal(account);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取用户默认印章地址
     *
     */
    @GetMapping("/seal/get")
    public ResponseEntity<String> getSeal(@NotBlank(message = "用户账号不能为空") String account) {
        String userSeals = caDoctorService.getUserSeals(account);
        return ResponseEntity.ok(userSeals);
    }

    /**
     * 上传待签文件
     *
     */
    @PostMapping("/contract/add")
    public ResponseEntity<Void> contractAdd(@Valid VisaWaitFileUploadReq req) {
        caDoctorService.createContract(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加签署方
     *
     */
    @PostMapping("/signer/add")
    public ResponseEntity<String> signerAdd(@Valid @RequestBody ConcatSignAddReq req) {
        String signer = caDoctorService.addSigner(req);
        return ResponseEntity.ok(signer);
    }

    /**
     * 下载签约文件合同
     *
     */
    @PostMapping("/contract/download")
    public ResponseEntity<String> downloadContract(@NotBlank(message = "合同编号不能为空") String contractNo) {
        String signer = caDoctorService.downloadContract(contractNo);
        return ResponseEntity.ok(signer);
    }


    /**
     * 大道处方签字, 测试接口url
     *
     */
    @PostMapping("/sign")
    public ResponseEntity<String> sign(@Valid VisaWaitFileUploadReq req) {

        String signer = caDoctorService.caSign(req);
        return ResponseEntity.ok(signer);
    }


}

